package com.ithuangyl.common.web.base;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.ithuangyl.common.web.callback.IWebViewInitializer;

import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;

/**
 * author : itHuang
 * e-mail : ithuangyl@163.com
 * date   : 2020/6/22 10:58
 */
public abstract class BaseWebViewActivity extends AppCompatActivity {

    private BaseWebView vWebView = null;
    private final ReferenceQueue<BaseWebView> WEB_VIEW_QUEUE = new ReferenceQueue<>();
    private String mUrl = null;
    private String mName = null;
    private boolean mIsWebViewAvailable = false;

    public static void show(Activity activity, Class<?> cls, String url, String name) {
        Intent intent = new Intent(activity, cls);
        intent.putExtra("url", url);
        intent.putExtra("name", name);
        activity.startActivity(intent);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.mUrl = getIntent().getStringExtra("url");
        this.mName = getIntent().getStringExtra("name");
        initWebEvent();
        initWebView();
        setContentView(vWebView);
        onBindView(savedInstanceState);
    }

    protected abstract void initWebEvent();

    public abstract IWebViewInitializer setInitializer();

    public abstract void onBindView(Bundle savedInstanceState);

    @SuppressLint("JavascriptInterface")
    private void initWebView() {
        if (vWebView != null) {
            vWebView.removeAllViews();
            vWebView.destroy();
        } else {
            final IWebViewInitializer initializer = setInitializer();
            if (initializer != null) {
                final WeakReference<BaseWebView> webViewWeakReference =
                        new WeakReference<>(new BaseWebView(this), WEB_VIEW_QUEUE);
                vWebView = webViewWeakReference.get();
                vWebView = initializer.initWebView(vWebView);
                vWebView.setWebViewClient(initializer.initWebViewClient());
                vWebView.setWebChromeClient(initializer.initWebChromeClient());
                vWebView.addJavascriptInterface(WebInterface.create(this), mName);
                mIsWebViewAvailable = true;
            } else {
                throw new NullPointerException("Initializer is null!");
            }
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (vWebView != null) {
            vWebView.onPause();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (vWebView != null) {
            vWebView.onResume();
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        mIsWebViewAvailable = false;
        if (vWebView != null) {
            vWebView.setWebViewClient(null);
            vWebView.setWebChromeClient(null);
            vWebView.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
            vWebView.clearHistory();
            vWebView.removeAllViews();
            vWebView.destroy();
            vWebView = null;
        }
    }

    public BaseWebView getWebView() {
        if (vWebView == null) {
            throw new NullPointerException("WebView IS NULL!");
        }
        return mIsWebViewAvailable ? vWebView : null;
    }

    public String getUrl() {
        if (mUrl == null) {
            throw new NullPointerException("mUrl IS NULL!");
        }
        return mUrl;
    }
}
